package com.atshiyou.web;

import com.atshiyou.bean.ZanNum;
import com.atshiyou.service.impl.ZanNumDaoServiceImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/numSub")
public class SubNumServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        ZanNumDaoServiceImpl zanNumDaoService = new ZanNumDaoServiceImpl();
        //1.获取参数
        String number = req.getParameter("number");
        int num1 = Integer.parseInt(number);
        System.out.println("获取页面的num："+num1);
        //处理逻辑
        num1--;
        //在数据库中更新数据
        boolean b = zanNumDaoService.updateNum(num1);
        if(b){
            System.out.println("数据修改成功");
        }else {
            System.out.println("数据并未修改哦");
        }
        //返回页面
        ZanNum zanNum = new ZanNum(num1);
        //转化成Json格式返回给客户端
        JSONObject jsonObject = JSONObject.fromObject(zanNum);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonObject);
    }
}
